<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\article\news;

use think\facade\Db;
use app\common\repositories\BaseRepository;
use app\common\dao\article\news\NewsCateDao;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class NewsCateRepository
 * @package app\common\repositories\article\news
 * @mixin NewsCateDao
 */
class NewsCateRepository extends BaseRepository
{

    public function __construct(NewsCateDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit,  int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->field('id,name,keywords,desc,sort,add_time,is_show')
            ->order('id', 'desc')
            ->select();
        return compact('count', 'list');
    }

    public function addInfo(array $data, int $propertyId = 1, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $data['property_id'] = $propertyId;
            $res = $this->dao->create($data);
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($propertyId, $userId, 'ARTICLE.NEWS.CATE.ADD', $data);
            return $res;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'ARTICLE.NEWS.CATE.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }


    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'ARTICLE.NEWS.CATE.DEL', $info);
        $this->dao->delete($info['id']);
    }

    public function getDetail(int $id)
    {
        return $this->dao->getSearch(['id' => $id])
            ->append(['picture'])
            ->find();
    }


    public function getCascaderData(int $propertyId = null, array $where = [])
    {
        $list = $this->dao->search($where, $propertyId)
            ->field('name,id')
            ->select()->toArray();
        return $list;
    }

}